package com.app.piemoment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.app.piemoment.config.ApiConfig;
import com.app.piemoment.entity.LoginInfo;
import com.app.piemoment.entity.PackageCallback;
import com.app.piemoment.log.PMLog;
import com.app.piemoment.net.HttpResultJson;
import com.app.piemoment.net.NetService;
import com.app.piemoment.util.StringUtils;

public class ActiveActivity extends Activity {
	public int sex = 1;
	EditText active_edittext;
	SharedPreferences userInfo;
	Button ok_Button;
	String activeCode;
	public String phone, password;
	private String PREF_NAME = "PREF_NAME";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.active_info);
		// phone = getIntent().getStringExtra("phone");

		userInfo = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
		phone = userInfo.getString("phone", "0");
		password = userInfo.getString("password", "0");
		Log.e("phone", phone);
		Log.e("password", password);
		// sex_EditText = (EditText) findViewById(R.id.sex_edittext);
		active_edittext = (EditText) findViewById(R.id.active_edittext);

		ok_Button = (Button) findViewById(R.id.ok_button);
		ok_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				activeCode = active_edittext.getText().toString();

				if (StringUtils.isNullOrEmpty(activeCode)) {
					Toast.makeText(ActiveActivity.this, "信息输入不完整，请检查",
							Toast.LENGTH_LONG).show();
					return;
				}

				RegAsy regAsy = new RegAsy();
				regAsy.execute();

			}
		});

	}

	class RegAsy extends AsyncTask<Object, Object, LoginInfo> {
		ProgressDialog bDialog = ProgressDialog.show(ActiveActivity.this,
				"请稍候", "正在验证");
		JSONObject jsonObject;
		PackageCallback packageCallback;

		@Override
		protected LoginInfo doInBackground(Object... param) {

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("_format", "json"));
			params.add(new BasicNameValuePair(ApiConfig.DEFAULT_TOKENS,
					ApiConfig.DEFAULT_TOKEN_VALUE));
			params.add(new BasicNameValuePair("mobile_phone", phone));
			params.add(new BasicNameValuePair("security_code", active_edittext
					.getText().toString()));

			HttpResultJson result = NetService.httpPostReturnJson(
					ActiveActivity.this, ApiConfig.URL_ROOT
							+ ApiConfig.ACTIVE_URL, params);
			jsonObject = result.getJson();
			if (jsonObject == null) {
				packageCallback = (PackageCallback) new PackageCallback() {

					@Override
					public PackageCallback parseItem(JSONObject jsonObject) {

						return null;
					}
				};
				packageCallback.isError = true;
				packageCallback.errorMsg = "数据有误";
			} else {
				packageCallback = (PackageCallback) new PackageCallback() {

					@Override
					public PackageCallback parseItem(JSONObject jsonObject) {
						return null;
					}
				}.parse(jsonObject);
			}

			return null;
		}

		@Override
		protected void onCancelled() {
			bDialog.dismiss();
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(LoginInfo result) {
			bDialog.dismiss();

			if (packageCallback.status == null) {
				PMLog.e("errorMsg", "erroMsg is " + packageCallback.getErrorMsg());
				Toast.makeText(ActiveActivity.this,
						packageCallback.getErrorMsg(), Toast.LENGTH_LONG)
						.show();

			} else {
				Toast.makeText(ActiveActivity.this, "激活成功", Toast.LENGTH_LONG)
						.show();
				LoginAsy loginAsy = new LoginAsy();
				loginAsy.execute();
			}
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			bDialog.show();
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			super.onProgressUpdate(values);
		}

	}

	class LoginAsy extends AsyncTask<Object, Object, LoginInfo> {
		ProgressDialog bDialog = ProgressDialog.show(ActiveActivity.this,
				"请稍候", "激活成功，自动登陆中....");

		@Override
		protected LoginInfo doInBackground(Object... param) {

			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("_format", "json"));
			params.add(new BasicNameValuePair(ApiConfig.USERKEY, phone));
			params.add(new BasicNameValuePair(ApiConfig.DEFAULT_TOKENS,
					ApiConfig.DEFAULT_TOKEN_VALUE));
			params.add(new BasicNameValuePair("passwd", password));
			HttpResultJson result = NetService.httpPostReturnJson(
					ActiveActivity.this, ApiConfig.URL_ROOT
							+ ApiConfig.LOGIN_URL, params);
			LoginInfo.getInstance().parse(result.getJson());
			Log.e("token", LoginInfo.getInstance().getAccess_token());
			Log.e("puid", LoginInfo.getInstance().getPuid() + "");

			return null;
		}

		@Override
		protected void onCancelled() {
			bDialog.dismiss();
			super.onCancelled();
		}

		@Override
		protected void onPostExecute(LoginInfo result) {
			bDialog.dismiss();
			if (LoginInfo.getInstance().isError) {
				Toast.makeText(ActiveActivity.this, "登录失败，请稍后再试。",
						Toast.LENGTH_LONG).show();
				Intent intent = new Intent();
				intent.setClass(ActiveActivity.this, LoginUserActivity.class);
				startActivity(intent);

			} else {
				if (LoginInfo.getInstance().getAccess_token() != null) {
					Toast.makeText(ActiveActivity.this, "登录成功，请您完善您的个人信息。",
							Toast.LENGTH_LONG).show();
					Intent intent = new Intent();
					intent.setClass(ActiveActivity.this,
							ComplateUserBaseActivity.class);
					startActivity(intent);
				}
			}
			super.onPostExecute(result);
		}

		@Override
		protected void onPreExecute() {
			bDialog.show();
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			super.onProgressUpdate(values);
		}

	}
}
